package br.com.emailUtil;

import java.util.Properties;

import javax.activation.DataHandler;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;


public class EmailSendBean   {
	
	

	public EmailSendBean() throws NoSuchProviderException {
		super();	
	}
	
	private Message message;
	private Transport transport;
	private Multipart corpo = new MimeMultipart(); 
	
	
	/** DESTINATARIO **/	 
	public void addDestinatario(String destinario) throws AddressException, MessagingException{
		 getMessage().setRecipient
		     (Message.RecipientType.TO, new InternetAddress(destinario));
	}
	
	/** ASSUNTO **/	 
	public void addAssunto(String assunto) throws AddressException, MessagingException{
		 getMessage().setSubject(assunto);
	}
	
	/** CORPO **/	 
	public void addCorpoTexto(String texto) throws AddressException, MessagingException{
		 BodyPart body = new MimeBodyPart();
	     body.setContent(texto, "text/html");
	     corpo.addBodyPart(body);
	}
	
	/** ANEXOS **/	 
	public void addAnexos(byte[] anexo, String nome) throws AddressException, MessagingException{
		String tipo = "text/html";
		
		if (nome.contains(".pdf")) {
			tipo = "application/pdf";
		}
		
		MimeBodyPart partAnexo = new MimeBodyPart();  
		partAnexo.setDataHandler(new DataHandler(new ByteArrayDataSource(anexo, tipo)));  
		partAnexo.setFileName(nome);  
        
        corpo.addBodyPart(partAnexo);	       
	}
	
	/** ENVIAR **/	 
	public void enviar() throws AddressException, MessagingException{
		getMessage().setContent(corpo);
		getMessage().saveChanges();
		
		getTransport().connect();
		getTransport().sendMessage(getMessage(), getMessage().getAllRecipients());
	}
	
	public void inicialize() throws NoSuchProviderException {
		
		
		
		Properties props = new Properties();
		
		
		/** PROPRIEDADES **/
        props.setProperty("mail.smtp.host", "smtp.gmail.com"); 
        props.setProperty("mail.smtp.user", "pauser2200@gmail.com" ); 
        props.setProperty("mail.smtp.port", "587"); 
        props.setProperty("mail.smtp.socketFactory.port", "587"); 
        props.setProperty("mail.transport.protocol", "smtp"); 
        props.setProperty("mail.smtp.starttls.enable","true"); 
        props.setProperty("mail.smtp.auth", "true"); 
        props.setProperty("mail.debug", "true");
        props.setProperty("mail.mime.charset", "ISO-8859-1");        
        props.setProperty("mail.smtp.socketFactory.fallback","true");
        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.timeout", "10000");
        
        	
        /** AUTENTICACAO **/
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                		"pauser2200@gmail.com", "ter71395"
                 );
            }
        };
        
        Session session = Session.getInstance(props, auth);
        session.setDebug(true); 
        
        /** MESSAGE **/
        setMessage(new MimeMessage(session));
        
        /** TRANSPORT **/
        transport = session.getTransport("smtp");
       
	}
	
	public Message getMessage() {
		return message;
	}


	public void setMessage(Message message) {
		this.message = message;
	}


	public Transport getTransport() {
		return transport;
	}


	public void setTransport(Transport transport) {
		this.transport = transport;
	}

	public Multipart getCorpo() {
		return corpo;
	}

	public void setCorpo(Multipart corpo) {
		this.corpo = corpo;
	}
	
	public static void main(String[] args) throws  AddressException, MessagingException {
		
		EmailSendBean emailSendBean = new EmailSendBean();
		
		emailSendBean.inicialize();
		emailSendBean.addAssunto("Meu assunto");
		emailSendBean.addDestinatario("pauser2200@gmail.com");
		
		emailSendBean.enviar();
	}
	
}
